﻿using System.IO;
using System.Windows.Forms;
using System.Xml.Linq;
using Portal.Helpers;
using Styx;
using Styx.Common;
using Styx.Helpers;

namespace Portal.Settings
{
    public class PortalSettings : Styx.Helpers.Settings
    {
        public static readonly PortalSettings Instance = new PortalSettings();

        private PortalSettings()
            : base(SettingsPath + ".xml")
        {
        }

        public static void SaveFile()
        {
            var saveFileDialog = new SaveFileDialog
                {
                    Filter = "Settings File|*.*",
                    Title = "Save Settings...",
                    InitialDirectory = string.Format("{0}\\Routines\\Portal\\Settings\\", Utilities.AssemblyDirectory),
                    DefaultExt = "xml",
                    FileName = "Portal - " + StyxWoW.Me.Name
                };
            var result = saveFileDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                Instance.SaveToFile(saveFileDialog.FileName);
                LogSettings();
                Log.Info("Settings Saved to File.");
            }
            else
            {
                Log.Info("Setting Save Cancelled.");
            }
        }

        public static void LoadFile(string filePath = null)
        {
            if (filePath == null)
            {
                var openFileDialog = new OpenFileDialog
                    {
                        Filter = "Settings File|*.*",
                        Title = "Load Settings...",
                        InitialDirectory =
                            string.Format("{0}\\Routines\\Portal\\Settings\\", Utilities.AssemblyDirectory)
                    };
                openFileDialog.ShowDialog();

                if (!openFileDialog.FileName.EndsWith(".xml")) return;
                Instance.LoadFromXML(XElement.Load(openFileDialog.FileName));
                Log.Info("Loaded Settings From File.");
            }
        }

        private static string SettingsPath
        {
            get
            {
                return string.Format("{0}\\Settings\\Portal\\Portal_Active_Settings_{1}", Utilities.AssemblyDirectory,
                                     StyxWoW.Me.Name);
            }
        }

        public static void LogSettings()
        {
            Log.Debug("");
            Log.Debug("====== Portal Settings ======");
            foreach (var kvp in Instance.GetSettings())
            {
                Log.Debug("+ {0}: {1}", kvp.Key, kvp.Value.ToString());
            }
            Log.Debug("==============================");
            Log.Debug("");
        }

        public void NonStaticLogSettings()
        {
            Log.Debug("");
            Log.Debug("====== Portal Settings ======");
            foreach (var kvp in Instance.GetSettings())
            {
                Log.Debug("+ {0}: {1}", kvp.Key, kvp.Value.ToString());
            }
            Log.Debug("==============================");
            Log.Debug("");
        }


        #region Rest Behavior

        [Setting, DefaultValue(true)]
        public bool AllowRestBehavior { get; set; }

        [Setting, DefaultValue(75)]
        public int EatHPPercent { get; set; }

        [Setting, DefaultValue(50)]
        public int DrinkManaPercent { get; set; }

        #endregion Rest Behavior

        #region Items
        [Setting, DefaultValue(true)]
        public bool CreateHealthstones { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseHealthstones { get; set; }

        [Setting, DefaultValue(false)]
        public bool OnlyHealthstoneWithDarkRegen { get; set; }

        [Setting, DefaultValue(30)]
        public int HealthstoneHPPercent { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseTrinket1 { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseTrinket2 { get; set; }

        /* Trinket Condition States
        0 = Never
        1 = On Boss Or Player
        2 = On Cooldown
        3 = On Loss of Control
        4 = Low Health 
        5 = Low Mana
         */

        [Setting, DefaultValue(0)]
        public int Trinket1Condition { get; set; }

        [Setting, DefaultValue(0)]
        public int Trinket2Condition { get; set; }

        [Setting, DefaultValue(30)]
        public int Trinket1LowHPValue { get; set; }

        [Setting, DefaultValue(30)]
        public int Trinket1LowManaValue { get; set; }

        [Setting, DefaultValue(30)]
        public int Trinket2LowHPValue { get; set; }

        [Setting, DefaultValue(30)]
        public int Trinket2LowManaValue { get; set; }

        #endregion Items

        #region Professions
        /* Synapse Springs
        0 = Never
        1 = On Boss Or Player
        2 = On Cooldown
         */
        [Setting, DefaultValue(false)]
        public bool UseSynapseSprings { get; set; }

        [Setting, DefaultValue(0)]
        public int SynapseSpringsCond { get; set; }


         /* Lifeblood
         0 = On Low HP
         1 = On Boss Or Player
         2 = On Cooldown
         */
        [Setting, DefaultValue(false)]
        public bool UseLifeblood { get; set; }

        [Setting, DefaultValue(1)]
        public int LifebloodCond { get; set; }

        [Setting, DefaultValue(25)]
        public int LifebloodLowHPValue { get; set; }

        #endregion

        #region Racials
        [Setting, DefaultValue(1)]
        public int RacialUsage { get; set; }

        #endregion Racials

        #region General - Non Specialization Abilities

        // Totems
        [Setting, DefaultValue(false)]
        public bool KillTotems { get; set; }

        [Setting, DefaultValue(0)]
        public int MageArmor { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseConeOfCold { get; set; }

        [Setting, DefaultValue(false)]
        public bool ConeOfColdOnlyWhenNotSlowed { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseDeepFreeze { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseSpellsteal { get; set; }

        // Alter Time
        [Setting, DefaultValue(true)]
        public bool UseAlterTimeOffensive { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseAlterTimeDefensive { get; set; }


        [Setting, DefaultValue(true)]
        public bool UseFrostNova { get; set; }

        [Setting, DefaultValue(true)]
        public bool OnlyFrostNovaWhenTargetingMe { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseIceBlock { get; set; }

        [Setting, DefaultValue(15)]
        public int IceBlockLowHpValue { get; set; }

        // Polymorph
        [Setting, DefaultValue(true)]
        public bool PolymorphAny { get; set; }

        [Setting, DefaultValue(true)]
        public bool PolymorphFocus { get; set; }

        [Setting, DefaultValue(true)]
        public bool PolymorphHealer { get; set; }

        // Counterspell
        [Setting, DefaultValue(true)]
        public bool CounterspellHealer { get; set; }

        [Setting, DefaultValue(true)]
        public bool CounterspellCaster { get; set; }

        [Setting, DefaultValue(true)]
        public bool CounterspellTarget { get; set; }

        [Setting, DefaultValue(true)]
        public bool CounterspellFocus { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseEvocationLowMana { get; set; }

        [Setting, DefaultValue(15)]
        public int EvocationLowManaPercent { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseEvocationLowHP { get; set; }

        [Setting, DefaultValue(30)]
        public int EvocationLowHPPercent { get; set; }

        //Never
        //On Cooldown
        //On Boss Or Player
        //On Target Low HP
        [Setting, DefaultValue(2)]
        public int MirrorImageCondition { get; set; }

        [Setting, DefaultValue(40)]
        public int MirrorImageTargetLowHpValue { get; set; }

        // Blink
        [Setting, DefaultValue(true)]
        public bool UseBlinkLowHp { get; set; }

        [Setting, DefaultValue(80)]
        public int BlinkLowHpValue { get; set; }

        [Setting, DefaultValue(true)]
        public bool BlinkForwards { get; set; }

        [Setting, DefaultValue(false)]
        public bool BlinkBackwards { get; set; }

        [Setting, DefaultValue(false)]
        public bool BlinkTowardsTeamMember { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseBlinkStunnedRooted { get; set; }

        [Setting, DefaultValue(true)]
        public bool DecurseSelf { get; set; }

        [Setting, DefaultValue(true)]
        public bool DecurseFocus { get; set; }

        [Setting, DefaultValue(false)]
        public bool DecurseAll { get; set; }

        [Setting, DefaultValue(true)]
        public bool ConjureManaGem { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseManaGem { get; set; }

        [Setting, DefaultValue(75)]
        public int ManaGemLowManaValue { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseArcaneBrilliance { get; set; }

        [Setting, DefaultValue(true)]
        public bool ConjureRefreshments { get; set; }

        #endregion General - Non Specialization Abilities

        #region Talents
        // Tier 1
        [Setting, DefaultValue(true)]
        public bool UsePresenceOfMind { get; set; }

        //On Target Low HP
        //With Polymorph When Someone Healing
        //On Cooldown
        //On Boss Or Player
        //Fire: Use With PvE Burst Sequence
        [Setting, DefaultValue(0)]
        public int PresenceOfMindCondition { get; set; }

        [Setting, DefaultValue(20)]
        public int PresenceOfMindTargetLowHPValue { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseBlazingSpeed { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseIceFloes { get; set; }


        // Tier 2
        [Setting, DefaultValue(true)]
        public bool UseTemporalShieldLowHp { get; set; }

        [Setting, DefaultValue(60)]
        public int UseTemporalShieldLowHpValue { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseTemporalShieldWhenCcd { get; set; }

        //Always
        //Combat
        //Low HP
        //Never
        [Setting, DefaultValue(1)]
        public int IceBarrierCondition { get; set; }

        [Setting, DefaultValue(80)]
        public int IceBarrierLowHpValue { get; set; }


        // Tier 3

        //On Cooldown (and target isn't boss)
        //On Me When Unit In Melee Range
        //On Target When <X> Units In Range
        //Never
        [Setting, DefaultValue(0)]
        public int RingOfFrostCondition { get; set; }

        [Setting, DefaultValue(2)]
        public int RingOfFrostUnitsNearTarget { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseIceWard { get; set; }

        [Setting, DefaultValue(true)]
        public bool IceWardOnMe { get; set; }

        [Setting, DefaultValue(true)]
        public bool IceWardOnTargetsTarget { get; set; }

        //On Target When Casting
        //On Anyone Casting
        //On Anyone Casting Healing Spell
        //Never
        [Setting, DefaultValue(0)]
        public int FrostjawCondition { get; set; }

        // Tier 4

        [Setting, DefaultValue(false)]
        public bool UseColdSnapLowHp { get; set; }

        [Setting, DefaultValue(20)]
        public int ColdSnapLowHpValue { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseColdSnapIceBlockOnCdAndUnder70Hp { get; set; }

        // Tier 6
        [Setting, DefaultValue(true)]
        public bool UseInvocation { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseRuneOfPower { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseIncantersWardOnLowHP { get; set; }

        [Setting, DefaultValue(80)]
        public int UseIncantersWardOnLowHPValue { get; set; }

        [Setting, DefaultValue(true)]
        public bool UseIncantersWardOnUnitsTargetingMe { get; set; }

        [Setting, DefaultValue(2)]
        public int UseIncantersWardOnUnitsTargetingMeValue { get; set; }

        #endregion Talents

        #region Frost
        [Setting, DefaultValue(true)]
        public bool Frost_EnableAoE { get; set; }

        [Setting, DefaultValue(4)]
        public int Frost_AoEUnitCount { get; set; }


        [Setting, DefaultValue(true)]
        public bool Frost_UsePetFreeze { get; set; }

        [Setting, DefaultValue(true)]
        public bool Frost_FrozenOrbOnCooldown { get; set; }

        [Setting, DefaultValue(true)]
        public bool Frost_FrozenOrbOnTargetLowHP { get; set; }

        [Setting, DefaultValue(60)]
        public int Frost_FrozenOrbOnTargetLowHPValue { get; set; }

        [Setting, DefaultValue(true)]
        public bool Frost_FrozenOrbTargetStunnedRooted { get; set; }

        //Never
        //On Cooldown
        //On Boss Or Player
        //On Target Low HP
        //On Boss Or Player + BF/FoF Aura
        [Setting, DefaultValue(4)]
        public int IcyVeinsCondition { get; set; }

        [Setting, DefaultValue(70)]
        public int IcyVeinsLowHPValue { get; set; }

        #endregion

        #region Fire
        [Setting, DefaultValue(true)]
        public bool Fire_EnableAoE { get; set; }

        [Setting, DefaultValue(4)]
        public int Fire_AoEUnitCount { get; set; }

        [Setting, DefaultValue(true)]
        public bool Fire_CombustionCooldown { get; set; }

        [Setting, DefaultValue(false)]
        public bool Fire_CombustionPoMAlterTimeCombo { get; set; }

        #endregion

        #region Arcane
        [Setting, DefaultValue(true)]
        public bool Arcane_EnableAoE { get; set; }

        [Setting, DefaultValue(4)]
        public int Arcane_AoEUnitCount { get; set; }

        //Never
        //On Cooldown
        //On Boss Or Player
        //On Target Low HP
        [Setting, DefaultValue(2)]
        public int Arcane_ArcanePowerCondition { get; set; }

        [Setting, DefaultValue(70)]
        public int Arcane_ArcanePowerLowHPValue { get; set; }

        #endregion
    }
}
